package orderoid.soap.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import orderoid.singleton.SingletonClient;
import orderoid.soap.Tool.DeserialisationTool;
import orderoid.soap.classeCommune.Commande;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.util.Log;

public class webRechercherCommandeFromClient extends AsyncTask<String, Void, List<Commande>> {
	
	private static final String PARAM_IDCLIENT = "login";
	
	@Override
	protected List<Commande> doInBackground(String... params) {		
		List<Commande> listeCommande = new ArrayList<Commande>();


		SoapObject request = new SoapObject(
				orderoid.soap.constant.method.NAMESPACE,
				orderoid.soap.constant.method.METHOD_NAME_LISTECOMMANDE);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				orderoid.soap.constant.method.URL);

		request.addProperty(PARAM_IDCLIENT, SingletonClient.getInstance().getClient().getEmail());
		
		try {
			androidHttpTransport.call(orderoid.soap.constant.method.SOAP_ACTION_LISTECOMMANDE, envelope);
			
			@SuppressWarnings("unchecked")
			Vector<SoapObject> soapObjectResp = (Vector<SoapObject>) envelope.getResponse();
			
			for ( SoapObject soC : soapObjectResp)
			{
				listeCommande.add(DeserialisationTool.deserialisationCommande(soC));
			}
			
		} catch (Exception e) {
			Log.e("Erreur :",e.getMessage());
			listeCommande = null;
		}
		return listeCommande;
	}

}
